package com.chc.web.fee_power.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chc.utils.Msg;
import com.chc.web.fee_park.entity.FeePark;
import com.chc.web.fee_park.entity.FeeParkParm;
import com.chc.web.fee_power.entity.FeePower;
import com.chc.web.fee_power.entity.FeePowerParm;
import com.chc.web.fee_power.service.FeePowerService;
import com.chc.web.live_house.entity.LiveHouse;
import com.chc.web.live_house.service.LiveHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/01/17/15:21
 * @Description: 电费管理控制器
 */
@RestController
@RequestMapping("/api/feePower")
public class FeePowerController {

    @Autowired
    private FeePowerService feePowerService;

    @Autowired
    private LiveHouseService liveHouseService;

    /**
     * 我的电费
     * */
    @GetMapping("/getMyPowerFee")
    public Msg getMyPowerFee(FeePowerParm feePowerParm){
        //构造分页对象
        IPage<FeePower> page = new Page<>();
        page.setCurrent(feePowerParm.getCurrentPage());
        page.setSize(feePowerParm.getPageSize());
        //查询条件
        QueryWrapper<FeePower> query = new QueryWrapper<>();
        query.lambda().eq(FeePower::getUserId,feePowerParm.getUserId());
        IPage<FeePower> list = feePowerService.page(page, query);
        return Msg.success("查询成功",list);
    }

    /**
     * 新增
     * */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:feePower:add')")
    public Msg add(@RequestBody @Valid FeePower feePower){
        //根据房屋id查询正在使用该房间用户
        QueryWrapper<LiveHouse> query = new QueryWrapper<>();
        query.lambda().eq(LiveHouse::getHouseId,feePower.getHouseId())
                .eq(LiveHouse::getUseStatus,"0");
        LiveHouse house = liveHouseService.getOne(query);
        if (house == null){
            return Msg.error("该房屋没人使用，不能添加电费!");
        }
        List<FeePower> listPower = feePowerService.getListPower(feePower.getHouseId());
        if (listPower.size() == 0) {
            //把查询出来的用户id设置到电费实体里面
            feePower.setUserId(house.getUserId());
            feePowerService.saveFeePower(feePower);
            return Msg.success("新增电费成功!");
        }
        for (FeePower item : listPower) {
            if (item.getHouseId().equals(feePower.getHouseId())) {
                if (!item.getPayPowerMonth().equals(feePower.getPayPowerMonth())) {
                    //把查询出来的用户id设置到电费实体里面
                    feePower.setUserId(house.getUserId());
                    feePowerService.saveFeePower(feePower);
                    return Msg.success("新增电费成功!");
                }
                return Msg.error("该房屋本月电费已添加，不能重复操作");
            }
        }
        return Msg.error("新增电费失败");
    }

    /**
     * 编辑
     * */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:feePower:edit')")
    public Msg edit(@RequestBody @Valid FeePower feePower){
        //根据房屋id查询正在使用该房间用户
        QueryWrapper<LiveHouse> query = new QueryWrapper<>();
        query.lambda().eq(LiveHouse::getHouseId,feePower.getHouseId())
                .eq(LiveHouse::getUseStatus,"0");
        LiveHouse house = liveHouseService.getOne(query);
        if (house == null){
            return Msg.error("该房间没人使用，不能编辑电费!");
        }
        //把查询出来的用户id设置到电费实体里面
        feePower.setUserId(house.getUserId());
        feePowerService.updateFeePower(feePower);
        return Msg.success("编辑电费成功!");
    }

    /**
     * 删除
     * */
    @DeleteMapping("/{powerId}")
    @PreAuthorize("hasAuthority('sys:feePower:delete')")
    public Msg delete(@PathVariable("powerId") Long powerId){
        //如果已经缴费，就不能删除
        QueryWrapper<FeePower> query = new QueryWrapper();
        query.lambda().eq(FeePower::getPowerId,powerId).eq(FeePower::getPayPowerStatus,"1");
        FeePower one = feePowerService.getOne(query);
        if (one != null){
            return Msg.error("已缴费，不能删除!");
        }
        //删除操作
        boolean b = feePowerService.removeById(powerId);
        if (b){
            return Msg.success("删除电费成功!");
        }
        return Msg.error("删除电费失败!");
    }

    /**
     * 列表查询
     * */
    @GetMapping("/list")
    public Msg getList(@Valid FeePowerParm parm){
        IPage<FeePower> list = feePowerService.getList(parm);
        return Msg.success("查询成功",list);
    }

    /**
     * 缴费
     * */
    @PostMapping("/payPower")
    public Msg payPower(@RequestBody FeePower feePower){
        boolean b = feePowerService.updateById(feePower);
        if (b){
            return Msg.success("缴费成功");
        }
        return Msg.error("缴费失败");
    }
}
